// components/addr.js
var addrbill = require('../../../../model/shop_clubaddr.js')
var actmember = require('../../../../model/member')
var acttoolv2 = require('../../../../sys/toolv2')
var userlocationv2 = require('../../../../sys/location/userlocationv2')

const app = getApp()

Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },

  /**
   * 组件的属性列表
   */
  properties: {
    xtid: { // 1社区团长点，2到店自提点
      type: Number,
      value: 1
    },
    showselected: { // 属性名
      type: Boolean,
      value: true
    },
    selectdefault: { // 属性名
      type: Boolean,
      value: true
    },
    delivery_config: {
      type: Object,
      value: null
    },
    themcolor: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    actid: -1, // 选中团长
    isshowpickuptime: false,
    // location:"请定位",
    addrmodel: {},
    region: [],
    addrblock: true,
    addrlist: [],
    op: "result",
    CustomBar: app.globalData.CustomBar,
    scroll: false,
    useraddr:null,
    userset: {
      pickuptime: null,
      name: "",
      phone: "",
      clubdelivery: "1"
    },
    vildfield: {
      name: true,
      phone: true
    },
    pageshow: true,
    basicurl: app.globalData.basicurl
  },
  ready: function () {


 
  
  

  },
  /**
   * 组件的方法列表
   */
  methods: {
    chooseLocation(ev) {
      var address = this.selectComponent("#address");
      address.chooseLocation(ev)
    },

     
    vliduserset: function (field) {
      var updatemodel = {}
      if (field) {
        updatemodel['vildfield.' + field] = app.globalData.userset[field] ? true : false;
      }
      else {
        ["name", "phone"].forEach(item => {
          updatemodel['vildfield.' + item] = app.globalData.userset[item] ? true : false;
        })
      }
      this.setData(updatemodel)
    },

    inputname: function (e) {
      var dataset = e.target.dataset
      app.globalData.userset[dataset.field] = e.detail
      this.vliduserset(dataset.field)
    },
    //更新自取时间
    confirmdatetime: function (e) {
      app.globalData.userset.pickuptime = e.detail
      var updatemodel = { isshowpickuptime: false }
      updatemodel["userset.pickuptime"] = e.detail
      this.setData({ isshowpickuptime: false, pickuptime: e.detail })
    },
    canceldatetime: function (e) {
      this.setData({ isshowpickuptime: false })
    },
    showpickertime: function () {
      this.setData({ isshowpickuptime: !this.data.isshowpickuptime })
    },




    //底部下拉
    onReachBottom: function (e) {
      var that = this;
      var strwhere = {};
      if (app.globalData.lat && app.globalData.lng) {
        strwhere['lat'] = app.globalData.lat;
        strwhere['lng'] = app.globalData.lng;
      }
      strwhere['xtid'] = this.data.xtid;
      if (this.data.keyval) {
        strwhere['keyval'] = this.data.keyval;
      }
      this.getpagelist(that, -1, strwhere);
    },
    //监听input
    watchinput: function (e) {

      this.data.keyval = e.detail;
      var that = this;
      var strwhere = {};
      if (app.globalData.lat && app.globalData.lng) {
        strwhere['lat'] = app.globalData.lat;
        strwhere['lng'] = app.globalData.lng;
      }
      strwhere['xtid'] = this.data.xtid;
      strwhere['keyval'] = e.detail;

      this.getpagelist(that, 1, strwhere);
    },
    //列表
    getpagelist(that, rest, strwhere) {
      if (rest == 1) {
        that.setData({
          addrlist: []
        });
      }
      addrbill.offset(that, rest, strwhere).then(function (result) {
        var updatemdoel = {};

        if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
          if (rest == 1) {
            updatemdoel["addrlist"] = result["obj"]["modellist"];
          } else {
            if (result["obj"]["modellist"].length > 0) {
              updatemdoel["addrlist"] = that.data.addrlist.concat(result["obj"]["modellist"]);
            }
          }
        }
        that.setData(updatemdoel);
      })
      return;
    },
    //返回
    Backfunc(e) {
      this.setData({
        op: "result"
      });
      
    },
    choseaddr: function () {
      var that =this;
      acttoolv2.forcechooseLocation().then(function (resut) {
        if (resut["status"] == 1) {  
          var addr = resut["obj"]
          var strwhere = {};
          strwhere['lat'] = addr.latitude;
          strwhere['lng'] = addr.longitude;
          strwhere['xtid'] = that.data.xtid;
          that.getpagelist(that, 1, strwhere)

          that.setData({
            op: "list",
            address: addr["address"],
            district: addr["xcaddr"][2]?addr["xcaddr"][2]:""
          })
        

        }


      })
    },
    //点击显示收货地址列表
    showselect: function (actid,useraddr) {  

    
      this.setData({
        useraddr:useraddr,
        actid: actid,
        keyval: ""
      })
      if(!useraddr)
      {
        this.getLocation();
      }
      else
      {

        var strwhere = {};
        strwhere['lat'] = useraddr.lat;
        strwhere['lng'] = useraddr.lng; 
        strwhere['pickmethod'] = 2; 
        var that= this;
        that.getpagelist(that, 1, strwhere)
        that.setData({
          op: "list"        
        })
      }

  return "Xshowselect";
    
    },
    getLocation: function () {
      var that = this;
      userlocationv2.lastaddr().then(function (resut) {        
        if (resut["status"] == 1) {
          var addr = resut["obj"]
          var strwhere = {};
          strwhere['lat'] = addr.lat;
          strwhere['lng'] = addr.lng;
          strwhere['xtid'] = that.data.xtid;
          that.getpagelist(that, 1, strwhere)
          that.setData({
            op: "list",
            address: addr["addrinfo"],
            district: addr["district"]
          })
          
        }
        return;

      })
    },
    loadpage(){

    },
    
 
    selectaddrfunc(ev) {

      var obj = { clubdelivery: this.data.userset.clubdelivery, obj: ev.detail }
      this.triggerEvent("shopdelivery", obj)
    },
    //选择当前地址
    selecteditem(e) {
      var dataset = e.currentTarget.dataset;
      var selectaddr = this.data.addrlist[dataset["idx"]];
     var  useraddr =this.data.useraddr

     if(useraddr){
        var delivery_config = this.data.delivery_config
        selectaddr["distance"]= app.toolv2.distanceByLnglat(selectaddr.lng, selectaddr.lat, useraddr.lng, useraddr.lat)
        if (selectaddr["distance"].val - delivery_config.intracity["maxdistance"] > 0) {
          wx.showToast({
            title: '配送范围之外',
          })
          return;
        }       
     } 
 
      this.setData({
        
        op: "result",
      })
     
      this.triggerEvent("selectgroupaddr", selectaddr)
     
    },

    scroll: function (e) {

      if (e.detail.scrollTop > 100) {
        this.setData({
          scroll: true
        })
      } else {
        this.setData({
          scroll: false
        })
      }
    },

    getmycloubinfo() {
      var app = getApp()
      if (app.globalData.clubinfo) {
        this.setData({
          clubinfo: app.globalData.clubinfo
        })
      } else {
        actmember.getmycloubinfo().then(res => {
          if (res["status"] == 1) {
            var clubinfo = res["obj"]
            if (app.globalData.lng) {
              var preslut = acttoolv2.distanceByLnglat(app.globalData.lng, app.globalData.lat, clubinfo.lng, clubinfo.lat)
              clubinfo["juli"] = preslut["formatval"] + preslut["unit"];
            }

            app.globalData.clubinfo = clubinfo;
            this.setData({
              clubinfo: clubinfo
            })
          }
        })
      }

    },
  }
})